<template>
  <nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType">
    <div class="site-navbar__header">
      <h1 class="site-navbar__brand" @click="$router.push({ name: 'home' })">
        <a class="site-navbar__brand-lg" href="javascript:;">{{config.siteSideBarName}}</a>
        <a class="site-navbar__brand-mini" href="javascript:;">{{config.abbName}}</a>
      </h1>
    </div>
    <div class="site-navbar__body clearfix">
      <!-- 折叠按钮 -->
      <el-menu class="site-navbar__menu" mode="horizontal">
        <el-menu-item class="site-navbar__switch" index="0" @click="sidebarFold">
          <icon-svg name="zhedie"></icon-svg>
        </el-menu-item>
        <el-menu-item class="site-navbar__switch" index="1" style="align-items: center">
          <breadcrumb />
        </el-menu-item>
      </el-menu>
      <!-- 靠右菜单 -->
      <el-menu class="site-navbar__menu site-navbar__menu--right" mode="horizontal">

        <!--不推荐有子菜单的菜单项-->

        <!--图表-->
        <!--<el-menu-item index="50"  @click="$router.push({ name: 'demo-echarts' })">-->
          <!--<template slot="title">-->
            <!--<el-tooltip content="图表" placement="bottom">-->
              <!--<icon-svg name="tubiao" class="site-sidebar__menu-icon" ></icon-svg>-->
            <!--</el-tooltip>-->
          <!--</template>-->
        <!--</el-menu-item>-->

        <!-- 消息提醒 -->
        <!--<el-menu-item index="3" @click="$router.push({ name: 'theme' })">-->
          <!--<el-tooltip content="消息" placement="bottom">-->
            <!--<el-badge value="5">-->
              <!--<icon-svg name="tixing" class="el-icon-setting"></icon-svg>-->
            <!--</el-badge>-->
          <!--</el-tooltip>-->
        <!--</el-menu-item>-->

        <el-menu-item index="50">
          <div class="interval"></div>
        </el-menu-item>

        <el-menu-item index="41">
          <el-tooltip content="切换语言" effect="light" placement="bottom">
            <language />
          </el-tooltip>
        </el-menu-item>

        <el-menu-item index="40">
          <el-tooltip content="设置主题" effect="light" placement="bottom">
            <settheme />
          </el-tooltip>
        </el-menu-item>

        <!--功能按钮:大小-->
        <el-menu-item index="30">
          <el-tooltip content="设置元素大小" effect="light" placement="bottom">
            <sizeselect />
          </el-tooltip>
        </el-menu-item>

        <!--功能按钮:全屏-->
        <el-menu-item index="20">
          <el-tooltip content="全屏" effect="light" placement="bottom">
            <screenfull />
          </el-tooltip>
        </el-menu-item>

        <!-- 设置 -->
        <!--<el-menu-item index="4" @click="$router.push({ name: 'theme' })">-->
          <!--<el-tooltip content="设置" placement="bottom">-->
            <!--<icon-svg name="shezhi" class="el-icon-setting"></icon-svg>-->
          <!--</el-tooltip>-->
        <!--</el-menu-item>-->

        <el-menu-item index="11">
          <div class="interval"></div>
        </el-menu-item>

        <!-- 个人中心 -->
        <el-menu-item class="site-navbar__avatar" index="10">
          <el-dropdown :show-timeout="0" placement="bottom" size="medium">
            <span class="el-dropdown-link">
              <img src="~@/assets/img/avatar.png" :alt="userName">
              <span>{{ userName }}</span>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="updatePasswordHandle()">个人资料</el-dropdown-item>
              <el-dropdown-item :divided="true" @click.native="updatePasswordHandle()">修改密码</el-dropdown-item>
              <el-dropdown-item @click.native="logoutHandle()">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
        <!--结束-->
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password v-if="updatePassowrdVisible" ref="updatePassowrd"></update-password>
  </nav>
</template>

<script>
  import UpdatePassword from './main-navbar-update-password'
  import Screenfull from '@/components/screen-full'
  import sizeselect from '@/components/size-select'
  import settheme from '@/components/set-theme'
  import breadcrumb from '@/components/bread-crumb'
  import language from '@/components/lang-select'
  import {clearLoginInfo} from '@/utils'

  // 导入theme：getter：
  // 1. 控制sidebar
  import { mapGetters } from 'vuex'

  export default {
    data () {
      return {
        updatePassowrdVisible: false
      }
    },
    components: {
      UpdatePassword,
      Screenfull,
      sizeselect,
      settheme,
      breadcrumb,
      language
    },
    computed: {
      // 添加sidebar计算属性
      ...mapGetters(['sidebar']),
      // 导航条类型
      navbarLayoutType: {
        get () {
          let type = this.$store.state.theme.navbarLayoutType.default
          // console.log('type', type)
          if (type) {
            return 'default'
          } else {
            return 'inverse'
          }
        }
      },
      // mainTabs: {
      //   get () {
      //     return this.$store.state.common.mainTabs
      //   },
      //   set (val) {
      //     this.$store.commit('common/updateMainTabs', val)
      //   }
      // },
      userName: {
        get () {
          return this.$store.state.user.name
        }
      }
    },
    methods: {
      // 触发sidebar状态改变
      sidebarFold () {
        this.$store.dispatch('toggleSideBar')
      },
      // 修改密码
      updatePasswordHandle () {
        this.updatePassowrdVisible = true
        this.$nextTick(() => {
          this.$refs.updatePassowrd.init()
        })
      },
      // 退出
      logoutHandle () {
        this.$confirm(`确定进行[退出]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/logout'),
            method: 'post',
            data: this.$http.adornData()
          }).then(({data}) => {
            if (data && data.code === 0) {
              clearLoginInfo()
              this.$router.push({name: 'login'})
            }
          })
        }).catch(() => {
        })
      }
    }
  }
</script>

<style>
  .top-menu-icon {
    width: 24px;
    margin-right: 5px;
    text-align: center;
    font-size: 20px;
    color: inherit !important;
  }

  .avator {
    width: 36px;
    height: auto;
    margin-right: 5px;
    border-radius: 100%;
    vertical-align: middle;
  }

  .interval {
    width:1px;
    height:38px;
    border-left: 1px solid #e6e6e6;
  }

</style>
